﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using QAHelper.Common;
using System.Web.UI;


namespace Logger
{
    public class HtmlLogger
    {
        public string logRoot = string.Empty;

        #region Commented Code

        /*public void WriteToFile(TestLog logList)
        {
            StringWriter strWriter = new StringWriter();
            using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter))
            {
                
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Html);
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Head);
                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Title);                
                            htmlTextWriter.Write("Smoke Test Log "+DateTime.Now.ToShortDateString());
                        htmlTextWriter.RenderEndTag(); // Title

                        #region Link tag for the CSS file

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Rel, "StyleSheet");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Href, "template.css");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Link);
                htmlTextWriter.RenderEndTag(); // Link

                #endregion

                    htmlTextWriter.RenderEndTag(); // Head

                    #region Body of the log

                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Body);

                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class,"resultset");
                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Ul);

                            htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class,"resultsetrow");
                            htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Li);

                                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class,"trow");
                                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
                                int i=1;
                                foreach (TestStep testStep in logList.testScenario.stepCollection)
                                    {
                                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id,"tid_"+i.ToString());
                                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class,"trowid");
                                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                                        htmlTextWriter.Write("T"+i.ToString());
                                        htmlTextWriter.RenderEndTag(); // trowid li

                                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id,"tdesc_"+i.ToString());
                                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                                            htmlTextWriter.Write(testStep.DESCRIPTION);
                                        htmlTextWriter.RenderEndTag(); // tdesc li


                                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id,"Li6"+i.ToString());
                                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                                            htmlTextWriter.Write(testStep.EXPECTED_RESULT);
                                        htmlTextWriter.RenderEndTag(); // Li6 li

                                        i++;

                                    }
                                    
                                htmlTextWriter.RenderEndTag(); // trow ul

                            htmlTextWriter.RenderEndTag(); // resultsetrow li
                
                        htmlTextWriter.RenderEndTag(); // resultset UL
                
                               

                htmlTextWriter.RenderEndTag(); // Body

                #endregion

                

                htmlTextWriter.RenderEndTag(); // Html

                File.WriteAllText(logRoot+"test.html", strWriter.ToString());
                
            }
            
        }*/

        #endregion

        public void WriteToFile(ArrayList testScenarios, string logDir, string logAssetUrl, string className)
        {
            StringWriter strWriter = new StringWriter();
            List<TestScenario> copiedList = CopyScenarioToTList(testScenarios);
            using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter))
            {               
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Html);
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Head);
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Title);
                htmlTextWriter.Write("Test_Log_Detail " + DateTime.Now.ToShortDateString());
                htmlTextWriter.RenderEndTag(); // Title

                #region Link tag for the CSS file

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Rel, "StyleSheet");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Href, logAssetUrl+"template.css");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Link);
                htmlTextWriter.RenderEndTag(); // Link

                #endregion

                #region Link tag for the JS file

                //htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.l, "StyleSheet");
                //htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.lan, logAssetUrl + "template.css");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Src, logAssetUrl + "log.js");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Script);
                htmlTextWriter.RenderEndTag(); // Link

                #endregion

                htmlTextWriter.RenderEndTag(); // Head

                #region Body of the log

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Body);

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "990");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "header");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Div);

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.H2);
                htmlTextWriter.Write("Test_Log_Detail " + DateTime.Now.ToShortDateString());
                htmlTextWriter.RenderEndTag(); // H2
                htmlTextWriter.RenderEndTag(); // DIV
                
                #region Summary Area

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.H1);
                htmlTextWriter.Write("Summary");
                htmlTextWriter.RenderEndTag(); // H1

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "2");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "2");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id, "logtable0");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Bgcolor, "#f0f2f5");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);


                #region Execution Area

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "150");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("TEST EXECUTED ON");
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write(DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToShortTimeString());
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "150");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("TEST EXECUTED BY");
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString());
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderEndTag(); // TR

                #endregion

                #region Test Count Area

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("TOTAL CASE");
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write(testScenarios.Count);
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("CASE EXECUTED");
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                int count = copiedList.FindAll
                                    (delegate(TestScenario sc)
                                    {
                                        return sc.SkipExecution == "0";
                                    }
                                   ).Count;
                htmlTextWriter.Write(count.ToString());
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderEndTag(); // TR

                #endregion

                #region PASS/FAIL Count Area

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("TOTAL PASS");
                htmlTextWriter.RenderEndTag(); // TD

                count = copiedList.FindAll(delegate(TestScenario sc)
                                           {
                                                return sc.Status == "Pass";
                                           }
                                           ).Count;

                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write(count.ToString());
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "summary");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write("TOTAL FAILED");
                htmlTextWriter.RenderEndTag(); // TD

                count = copiedList.FindAll(delegate(TestScenario sc)
                                          {
                                                return sc.Status == "Fail";
                                          }
                                          ).Count;

                
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlTextWriter.Write(count.ToString());
                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderEndTag(); // TR

                #endregion

                htmlTextWriter.RenderEndTag(); // Table

                #endregion


                #region Grid Header

                #region Header
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.H1);
                htmlTextWriter.Write("Detail");
                htmlTextWriter.RenderEndTag(); // H1
                #endregion

                #region Table

                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "2");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "2");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id, "logtable");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Bgcolor, "#f0f2f5");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Border, "1");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);

                #region Grid Header Row
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                
                #region ID TH
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "80");
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Th);
                htmlTextWriter.Write("ID");
                htmlTextWriter.RenderEndTag(); // TH
                #endregion

                #region Test Steps TH
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "400");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Th);
                htmlTextWriter.Write("Test Steps");
                htmlTextWriter.RenderEndTag(); // TH
                #endregion

                #region Expected Result TH
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "200");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Th);                
                htmlTextWriter.Write("Expected Result");
                htmlTextWriter.RenderEndTag(); // TH
                #endregion

                #region Status TH
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "80");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Th);                
                htmlTextWriter.Write("Status");
                htmlTextWriter.RenderEndTag(); // TH
                #endregion

                #region Actual Result TH
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Width, "80");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Th);                
                htmlTextWriter.Write("Actual Result");
                htmlTextWriter.RenderEndTag(); // TH
                #endregion

                htmlTextWriter.RenderEndTag(); // TR
                #endregion

                #endregion

                #endregion
                

                int i = 1;
                foreach (TestScenario testScenario in testScenarios)
                {

                    #region Each Row
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                
                    #region ID Column
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                    htmlTextWriter.Write(i.ToString());
                    htmlTextWriter.RenderEndTag(); // TD
                    #endregion

                    #region Test Steps
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "tdbg");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);                    

                    #region Test Steps DIV
                    

                    #region Test Description Div

                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "testname");
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Onclick, "toggleSteps("+"'t" + i.ToString() + "'"+")");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                    htmlTextWriter.Write(testScenario.TestObjective);
                    

                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "expan");
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id, "t" + i.ToString() + "_toggle");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                    htmlTextWriter.Write("-");
                    htmlTextWriter.RenderEndTag(); // Step DIV
                    htmlTextWriter.RenderEndTag(); // Step DIV
                    

                    #endregion

                    string[] steps = testScenario.TestSteps.Split('\n');

                    #region Div
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "cleartop");
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Id, "t" + i.ToString() + "_step");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                    #endregion

                    #region Table

                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "teststeps");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);
                    

                    #endregion

                    foreach(string step in steps)
                    {
  

                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                
                        #region Step Column
                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "steppass");
                        htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);                        
                        htmlTextWriter.Write(step);
                        htmlTextWriter.RenderEndTag(); // TD
                        #endregion
                        htmlTextWriter.RenderEndTag(); // TR

                    }
                    htmlTextWriter.RenderEndTag(); // Table
                    //htmlTextWriter.RenderEndTag(); // DIV

                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Script);
                    htmlTextWriter.Write("toggleSteps('t"+i.ToString()+"');");
                    htmlTextWriter.RenderEndTag(); // Script                 

                    #endregion

                    htmlTextWriter.RenderEndTag(); // TD
                    #endregion

                    #region Expected Result TD
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "tdbg");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);                    
                    htmlTextWriter.Write(testScenario.ExpectedResult);
                    htmlTextWriter.RenderEndTag(); // TD
                    #endregion

                    #region Status TD
                    htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                    if (testScenario.Status.Equals("Pass"))
                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "testpass");
                    else if (testScenario.Status.Equals("Fail"))
                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "testfail");
                    else if (testScenario.Status.Equals("Skipped"))
                        htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "testskip");
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);                                        
                    htmlTextWriter.RenderEndTag(); // TD
                    #endregion

                    #region Actual Result TD
                    htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                    htmlTextWriter.Write(testScenario.ObservedResult);
                    htmlTextWriter.RenderEndTag(); // TD
                    #endregion

                    htmlTextWriter.RenderEndTag(); //Each Row TR
                    #endregion

                    i++;

                }

                htmlTextWriter.RenderEndTag(); // Table

                htmlTextWriter.RenderEndTag(); // TR

                htmlTextWriter.RenderEndTag(); // TD

                htmlTextWriter.RenderEndTag(); // Table

                htmlTextWriter.RenderEndTag(); // Table

                htmlTextWriter.RenderEndTag(); // TR

                htmlTextWriter.RenderEndTag(); // TD


                htmlTextWriter.RenderEndTag(); // Body

                #endregion



                htmlTextWriter.RenderEndTag(); // Html

                string docTypeStr = "<!DOCTYPE html PUBLIC \""+" -//W3C//DTD XHTML 1.0 Transitional//EN\""+" "+"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

                File.WriteAllText(logDir + "/"+className+".html", docTypeStr+strWriter.ToString());

            }

        }
        public List<TestScenario> CopyScenarioToTList(ArrayList scenarios)
        {
            List<TestScenario> listScenario = new List<TestScenario>();
            foreach (TestScenario scenario in scenarios)
            {
                listScenario.Add(scenario);
            }

            return listScenario;
        }
    }
}
